#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Sanchez
#
# Created:     27/06/2012
# Copyright:   (c) Sanchez 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import socket
import sys
import time

def ScpiCommand(sCommand):
    s=sCommand+"\n"
    err=sock.sendall(s)
    return err



# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('169.254.241.19',5025)
sock.connect(server_address)
try:
    s = '*IDN?\n'
    sock.sendall(s)
    # Look for the response
    amount_received = 0
    amount_expected = len(s)

    while amount_received < amount_expected:
        data = sock.recv(8192)
        amount_received += len(data)
    print  data,len(data)

    err=ScpiCommand("*RST")
    print  err

    s=":AD:ENAB ON,(@1,2,3,4)"
    err=ScpiCommand(s)
    print  err

    type="DC"
    s=":AD:COUP "+type+",(@1,2,3,4)"
    err=ScpiCommand(s)
    print  err

    s=":AD:BUFF:MODE wrap"
    err=ScpiCommand(s)
    print  err

    level=1
    s=":AD:GAIN "+str(level)+",(@1,2,3,4)"
    err=ScpiCommand(s)
    print  err

    s=":AD:CLOCK:SOURCE INTERNAL"
    err=ScpiCommand(s)
    print  err

    freq=5000
    s=":AD:CLOC:FREQ "+str(freq)
    err=ScpiCommand(s)
    print  err

    s=":AD:TRIG IMM"
    err=ScpiCommand(s)
    print  err

    s=":AD:ARM"
    err=ScpiCommand(s)
    print  err

    s=":AD:INIT"
    err=ScpiCommand(s)
    print  err

    print "debut acquisition..."
    tacq=2
    time.sleep(5)
    print "debut acquisition..."

    # recup donnees
    s=":AD:FETCH? 0"
    err=ScpiCommand(s)
    print  err

    s=":AD:ABORT"
    err=ScpiCommand(s)
    print  err
    # Look for the response
    amount_received = 0
    amount_expected = len(s)

    while amount_received < amount_expected:
        data = sock.recv(8192)
        amount_received += len(data)
    print  data,len(data)
    nomfic="test.dat"
    fd = open(nomfic, 'wb')
    fd.write(data)
    fd.close()
finally:
    sock.close()

